package 数据结构.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {

        System.out.println("双向连表的测试");
        HeroNOde2 heroNOde1 = new HeroNOde2(1, "曹操", "孟德");
        HeroNOde2 heroNOde2 = new HeroNOde2(2, "刘备", "玄德");
        HeroNOde2 heroNOde3 = new HeroNOde2(3, "孙权", "仲谋");
        HeroNOde2 heroNOde4 = new HeroNOde2(4, "吕布", "奉先");
        //创建双向连表对象
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(heroNOde1);
        doubleLinkedList.add(heroNOde2);
        doubleLinkedList.add(heroNOde3);
        doubleLinkedList.add(heroNOde4);
        doubleLinkedList.list();

        //修改测试
        HeroNOde2 newNode = new HeroNOde2(4, "诸葛亮", "孔明");
        doubleLinkedList.update(newNode);
        doubleLinkedList.list();
        //删除测试
        doubleLinkedList.delete(1);
        doubleLinkedList.list();
    }

}


class DoubleLinkedList {
    private HeroNOde2 head = new HeroNOde2(0, "", "");

    //返回头节点的方法
    public HeroNOde2 getHead() {
        return head;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("连表为空");
            return;
        }
        HeroNOde2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * 添加
     *
     * @param heroNOde2
     */
    public void add(HeroNOde2 heroNOde2) {
        HeroNOde2 temp = head;
        //遍历连表,找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //当退出while循环,temp就指向了链表的最后
        //形成双向链表
        temp.next = heroNOde2;
        heroNOde2.pre = temp;

    }

    /**
     * 修改,与单向列表完全一样
     */
    public void update(HeroNOde2 newNOde2) {
        HeroNOde2 temp = head;
        boolean flag = false;
        while (true) {
            if (head.next == null) {
                System.out.println("连表是空");
                break;
            }
            if (temp.no == newNOde2.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newNOde2.name;
            temp.nickname = newNOde2.nickname;
        } else {
            System.out.printf("没有找到编号 %d 的节点,不能修改 \n", newNOde2.no);
        }
    }

    /**
     * 删除节点
     * 对于双项列表,可以直接找到要删除的这个节点,可以自我删除
     */
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("空连边");
            return;
        }
        HeroNOde2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            //此处有问题,如果是最后一个节点,就不需要执行下面这句代码;
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }

        }


    }

}

class HeroNOde2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNOde2 next;
    public HeroNOde2 pre;

    public HeroNOde2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNOde2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}




